package com.xiaoge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoge.config.SnowflakeIdWorker;
import com.xiaoge.exception.BusinessException;
import com.xiaoge.service.OssService;
import com.xiaoge.vo.EduLecturerVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoge.mapper.EduLecturerMapper;
import com.xiaoge.entity.EduLecturer;
import com.xiaoge.service.EduLecturerService;
import org.springframework.web.multipart.MultipartFile;

/**
* @Author: 冷俊杰
* @Description: 讲师信息
* @DateTime: 2022/10/28 12:02
**/
@Service
public class EduLecturerServiceImpl extends ServiceImpl<EduLecturerMapper, EduLecturer> implements EduLecturerService{
    @Resource
    private OssService ossService;
    //雪花工具类
    private SnowflakeIdWorker snowflakeIdWorker=  new SnowflakeIdWorker(2,2,2);

    @Override
    public Page<EduLecturer> getEduLecturerByPage(Page<EduLecturer> page, String lecturerName) {
        List<EduLecturer> eduLecturers = this.baseMapper.getEduLecturerList(page, lecturerName);
        if (eduLecturers.size()==0){
            throw new BusinessException("暂无数据");
        }
        //将刚刚动态sql查询的内容给page对象,让它去实现分页
        page.setRecords(eduLecturers);
        return page;
    }

    @Override
    public void addEduLecturer(EduLecturerVO eduLecturer, MultipartFile file) {
        String lecturerName = eduLecturer.getLecturerName();
        LambdaQueryWrapper<EduLecturer> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EduLecturer::getLecturerName,lecturerName);
        Long i = this.baseMapper.selectCount(wrapper);
        if(i!=0){
            throw new BusinessException("该讲师名称已被占用");
        }
        eduLecturer.setHeadImgUrl(ossService.upload(file));
        //通过雪花算法生成老师编号
        eduLecturer.setLecturerUserNo(snowflakeIdWorker.nextId());
        this.baseMapper.addEduLecturer(eduLecturer);
    }

    @Override
    public void updateeduLecturer(EduLecturerVO eduLecturer) {
        this.baseMapper.updateeduLecturer(eduLecturer);
    }
}
